import cTitle from "components/title";
import { Toast } from "vant";
import { scrollMixin } from "../../utils/mixin";
import recommended from "./component/recommended.vue";
import rated from "./component/rated.vue";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      toi: this.fun.getKeyByI(),
      selected: "0",
      //待评价
      wait: [],
      isLoadMore: true, //待评价是否能加载更多
      waitPage: 1,
      waitLastPage: 0,
      //已评价
      commentLength: '',
      waitLength:'',
      commentLoadMore: false, //已评价是否能加载更多
      commentPage: 1,
      commentLastPage: 0,
      //其他
      other: []
    };
  },
  methods: {
    swichTabTItem() {
      this.wait = [];
      this.isLoadMore = false;
      this.waitLastPage = 0;
      this.waitPage = 1;
      this.getNetData(this.selected);
    },
    initData() {
      this.commentLength = '';
      this.waitLength = '';
      this.toi = this.fun.getKeyByI();
      this.selected = "0";
      this.wait = [];
      this.isLoadMore = false;
      this.waitPage = 1;
      this.waitLastPage = 0;
      this.other = [];
    },
    //评价

    getNetData(status) {
      let json = {
        status: status,
        page: this.waitPage
      };
      $http
        .get("order.my-comment.paging", json, "获取中")
        .then(response => {
          if (response.result == 1) {
            this.isLoadMore = true;
            this.waitLastPage = response.data.list.last_page;
            let list = response.data?.list?.data || [];
            if (!this.waitLastPage) {
              this.waitLastPage = 0;
            }
            Number(status)===1?this.commentLength = list.length:this.waitLength = list.length;
            this.wait = list;
          } else {
            Toast(response.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.waitPage>= this.waitLastPage) {
        return;
      } else {
        this.waitPage += 1;
        let json = {
          status: Number(this.selected),
          page: this.waitPage
        };
        $http.post("order.my-comment.paging", json, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let data = res.data;
              let list = data.list.data;
              this.wait = [...this.wait, ...list]; //数组拼接
              Number(this.selected)===1?this.commentLength = this.wait.length:this.waitLength = this.wait.length;
            } else {
              this.waitLastPage = this.waitLastPage - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    },
  },
  activated() {
    this.initData();
    this.toi = this.fun.getKeyByI();
    this.getNetData(this.selected);
    this.$on("selected", text => {
      this.selected = text;
    });
  },
  created() {},
  components: { 
    cTitle,
    recommended,
    rated 
  }
};
